﻿using BencodeNET.Objects;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PrivateTracker.NET.Application;
using System;
using System.Threading.Tasks;
using System.Web;

namespace PrivateTracker.NET.Web.Entry.Controllers
{
    /// <summary>
    /// Tracker服务
    /// </summary>
    [ApiDescriptionSettings("Tracker", Order = 99999)]
    public class AnnounceController : IDynamicApiController
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IAnnounceService _announceService;
        public AnnounceController(IHttpContextAccessor httpContextAccessor, IAnnounceService announceService)
        {
            _httpContextAccessor = httpContextAccessor;
            _announceService = announceService;
        }
        /// <summary>
        /// Tracker地址
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("announce")]
        [NonUnify, AllowAnonymous]
        public async Task GetTaskAsync([FromQuery] AnnounceInput input)
        {
            var outpu = await _announceService.GetAnnounceOutputAsync(input);
            // 写入响应结果。
            var result = outpu.EncodeAsBytes();
            var response = _httpContextAccessor.HttpContext.Response;
            response.ContentType = "text/plain";
            response.StatusCode = 200;
            response.ContentLength = result.Length;
            await response.Body.WriteAsync(result);
        }
    }
}
